package com.xjtu.platform.security.register;


import com.xjtu.platform.common.CommonService;
import com.xjtu.platform.common.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Random;

@RestController
@RequestMapping("/security/register")
public class RegisterAPI {

    static Random r=new Random();
    static int sendcode;
    static int codestart;

    @Resource
    private FlagInsertService flagInsertService;

    @Resource
    private RegisterService registerService;

    @Resource
    private CodeIdentService codeIdentService;

    @Resource
    private RegisteService registeService;

    @Resource
    private CommonService commonService;

    @PostMapping("")
    public Result RegisteNew(@RequestBody RegisteDto registeDto){
        boolean ok = registeService.insertUser(registeDto);
        if(ok){
            return Result.success("注册基础信息成功!!!");
        }else {
            return Result.fail(Result.ERR_CODE_SYS,"系统维护中！！！！");
        }
    }

    @PostMapping("/registerOk")
    public Result RegisterUser(@RequestBody RegisterDto dto){
        String user_id = dto.getUser_id();
        long currentTime = System.currentTimeMillis();
        System.out.println(currentTime);
        long cachetime = registerService.getCachetime(user_id);
        if(currentTime - cachetime <= 600000) {
            boolean codeIdent = codeIdent(dto.getUserCode(), dto);
            if (codeIdent) {
                boolean ok = registerService.register(dto);
                if (ok) {
                    return Result.success("注册成功！！！");
                } else {
                    return Result.fail(Result.ERR_CODE_SYS, "系统维护中！！！");
                }
            } else {
                return Result.fail(Result.ERR_CODE_BUSINESS, "验证码错误");
            }
        }else {
            return Result.fail(Result.ERR_CODE_SYS, "验证码时间超过10分钟");
        }
    }

    @PostMapping("/sendmail")
    public Result SendMail(@RequestBody MailDto mailDto) {
        String user_id = mailDto.getUser_id();
        boolean exist = flagInsertService.getExist(mailDto);
        if(exist){
            return Result.fail(Result.ERR_CODE_BUSINESS,"邮箱已注册");
        }else {
            String mail = mailDto.getEmail();
            try {
                int code = sendemailcode(mail);
                boolean ok = flagInsertService.insertFlag(mailDto,code);
                commonService.setCache(user_id,"");
                return Result.success("发送成功");
            } catch (Exception e) {
                e.printStackTrace();
                return Result.fail(Result.ERR_CODE_SYS,"发送失败");
            }
        }
    }

    public static int sendemailcode(String targetmail){
        try {
            int code=r.nextInt(900000)+100000;
            codestart = (int) System.currentTimeMillis();
            SendMail.send(targetmail,"您的验证码为： \n"+ code+"\n"+"有效时间为10分钟");
            sendcode=code;
            return sendcode;

        } catch (Exception e) {
            e.printStackTrace();
            return 0;

        }

    }

    public boolean codeIdent(int userCode,RegisterDto registerDto){
        int code = codeIdentService.getCode(registerDto);
        if(code == userCode){
            return true;
        }else {
            return false;
        }
    }
}
